//
//  CYTransferCell.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/8.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTransferCell.h"
#import "CYTextField.h"
@interface CYTransferCell()<UITextFieldDelegate>
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYView *line;
@end
@implementation CYTransferCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:_title size:AL(PT(28)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(26)));
            make.centerY.equalTo(self);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYTextField *)textField{
    if (!_textField) {
        _textField = [[CYTextField alloc]initWithPlaceholder:nil color:ColorHex(0x888888) size:AL(PT(26))];
        _textField.delegate = self;
        _textField.textAlignment = NSTextAlignmentRight;
        [self addSubview:_textField];
        
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).with.offset(-AL(PT(26)));
            make.centerY.equalTo(self);
            make.left.equalTo(_titleLBL.mas_right).with.offset(AL(PT(26)));
            make.height.mas_equalTo(AL(PT(90)));
        }];
    }
    return _textField;
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:LINE_COLOR];
        [self addSubview:_line];
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 0.5));
        }];
    }
    return _line;
}
- (void)setTitle:(NSString *)title{
    if (_title != title)
        _title = title;
    self.titleLBL.hidden = NO;
    //    self.line.hidden = ([title isEqualToString:@"确认密码"])?NO:YES;
    self.line.hidden = NO;
    
}

- (void)setDict:(NSDictionary *)dict{
    if (_dict != dict)
        _dict = dict;
    _textField.text = [[dict allKeys] firstObject];
    _myblock(dict);
}

- (void)setText:(NSString *)text{
    if (_text != text)
        _text = text;
    _textField.text = text;
    if (![_title isEqualToString:@"存款金额"])
        _myblock(text);
}

- (void)setStyle:(NSString *)style{
    if (_style != style)
        _style = style;
    if ([style isEqualToString:@"selection"])
        [_textField setupRightview:@"zuola"];
}

#pragma mark - uitextfield代理
//下拉选择
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    _mylist( _title);
    if ([_style isEqualToString:@"selection"])
        return NO;
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField.text containsString:@" "])
        _myblock([textField.text stringByReplacingOccurrencesOfString:@" " withString:@""]);//去除空格
    else if ([textField.text containsString:@","])
        _myblock([textField.text stringByReplacingOccurrencesOfString:@"," withString:@""]);//去除逗号
    else
        _myblock(textField.text);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    _mylist(nil);
    [self endEditing:YES];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([_title containsString:@"银行卡号"]){
        NSString *new_string = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        textField.text = [NSString spaceBetweenFourCharacters:new_string];
        if (new_string.length >= 16 && string.length > 0)
            return NO;
    }
    else if ([_title containsString:@"存款金额"]){
        if (textField.text.length == 0 && [string isEqualToString:@"0"])//第一个数字不能为0
            return NO;
        else if (textField.text.length <= 8 && string.length > 0){//长度限制为8位数，即百万
            NSString *new_string = [textField.text stringByAppendingString:string];
            NSString *new_str = [new_string stringByReplacingOccurrencesOfString:@"," withString:@""];
            textField.text = [NSString commaBetweenThreeCharacters:new_str];
            return NO;
        }
        else if (textField.text.length > 8 && string.length > 0)//超过8位，禁止输入
            return NO;
        else//可以删除
            return YES;
    }
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
